﻿namespace WH.Common
{
    public class Respondence
    {
        /// <summary>
        /// 最多6位正整数。0：未作处理 1：成功 其它：失败
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// Code 不为“0”或“1”时的出错提示
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 记录总数。注意：（1）该数值不是Result节点中记录的数量；（2）仅在获取查询结果时有效
        /// </summary>
        public int RecordCount { get; set; }
        //public T Result;     //对象 返回的处理结果（失败或C、U、D操作为空）

        public Respondence()
        {
            Code = -1;
            Message = "初始化";
            RecordCount = 0;
            //Result = default(T);
        }

        public virtual void Fail(int code, string msg, Exception ex = null)
        {
            Code = code;
            if (ex == null)
            {
                msg = string.IsNullOrWhiteSpace(msg) ? "" : msg;
                Message = msg == "" ? "未知" : msg;
            }
            else
            {
                Message = msg + "异常" + ex.ToString();
            }
        }

        public virtual void Identical(Respondence source)
        {
            if (source != null)
            {
                Code = source.Code;
                Message = source.Message;
                RecordCount = source.RecordCount;
            }
        }

        public virtual void Succeed(int recordCount = 0, string msg = "")
        {
            Code = 1;
            Message = msg;
            RecordCount = recordCount;
        }

    }

    public class Respondence<T> : Respondence where T : class
    {
        //public int Code;     //最多6位正整数。0：未作处理 1：成功 其它：失败
        //public string Message; //Code 不为“0”或“1”时的出错提示
        //public int RecordCount; //记录总数。注意：（1）该数值不是Result节点中记录的数量；（2）仅在获取查询结果时有效

        /// <summary>
        /// 对象 返回的处理结果（失败或C、U、D操作为空）
        /// </summary>
        public T Result { get; set; }

        public Respondence()
        {
            Code = -1;
            Message = "";
            RecordCount = 0;
            Result = default;
        }

        public void Identical(Respondence<T> source)
        {
            Result = source.Result;
            base.Identical(source);
        }
    }


}